package io.xxx.mario.task.api;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.xxx.mario.common.DateFormats;
import io.xxx.mario.common.Validator;
import io.xxx.mario.task.ExecutionMode;
import io.xxx.mario.task.ScopeType;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Getter
@Setter
public abstract class AbstractTaskRequest implements Validator {

    /**
     * 名称
     */
    private String name;

    private ExecutionMode executionMode;

    /**
     * 开始时间
     */
    @JsonFormat(pattern = DateFormats.DEFAULT)
    private LocalDateTime startTime;

    /**
     * 结束时间
     */
    @JsonFormat(pattern = DateFormats.DEFAULT)
    private LocalDateTime endTime;

    /**
     * 发送范围
     */
    private ScopeType scopeType;

    private boolean startNow;

    /**
     * 待推送的消息
     */
    private List<MessageRequest> messages;

    private List<Long> roomIds;

    private int receiverCount;

    @Override
    public void validate() {
        if (StringUtils.hasLength(name)) {
            throw new IllegalArgumentException("任务名称为空");
        }
        if (executionMode == null) {
            log.warn("任务执行模式未空");
            executionMode = ExecutionMode.ONCE;
        }
        if (startTime == null) {
            throw new IllegalArgumentException("任务执行时间为空");
        }
        if (scopeType == null) {
            throw new IllegalArgumentException("任务发送范围为空");
        }
        if (ObjectUtils.isEmpty(messages)) {
            throw new IllegalArgumentException("任务消息列表为空");
        }
        if (receiverCount <= 0) {
            throw new IllegalArgumentException("任务接收人数为0");
        }
    }
}
